Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 491)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.36939 11.42569 11.48100 11.53533 11.58868 11.64107 11.69250 11.74298
##   [9] 11.79251 11.84110 11.88877 11.93551 11.98134 12.02626 12.07028 12.11341
##  [17] 12.15565 12.19701 12.23751 12.27714 12.31590 12.35378 12.39075 12.42681
##  [25] 12.46194 12.49611 12.52932 12.56156 12.59280 12.62303 12.65224 12.68041
##  [33] 12.70752 12.73356 12.75872 12.78316 12.80682 12.82967 12.85166 12.87273
##  [41] 12.89285 12.91196 12.93001 12.94696 12.96277 12.97737 12.99074 13.00281
##  [49] 13.01393 13.02443 13.03424 13.04328 13.05149 13.05878 13.06508 13.07033
##  [57] 13.07445 13.07736 13.07899 13.07927 13.07813 13.07549 13.07014 13.06119
##  [65] 13.04903 13.03408 13.01675 12.99743 12.97654 12.95448 12.93167 12.90849
##  [73] 12.88537 12.86270 12.84090 12.82037 12.80152 12.78475 12.76703 12.74547
##  [81] 12.72070 12.69336 12.66408 12.63351 12.60227 12.57100 12.54034 12.51092
##  [89] 12.48337 12.45833 12.43644 12.41833 12.40153 12.38336 12.36413 12.34416
##  [97] 12.32375 12.30323 12.28291 12.26310 12.24412 12.22628 12.20989 12.19528
## [105] 12.18275 12.17261 12.16501 12.15968 12.15632 12.15463 12.15432 12.15509
## [113] 12.15665 12.15870 12.16094 12.16309 12.16484 12.16589 12.16596 12.16474
## [121] 12.16304 12.16184 12.16113 12.16089 12.16111 12.16177 12.16286 12.16435
## [129] 12.16624 12.16850 12.17112 12.17408 12.17737 12.18097 12.18487 12.18904
## [137] 12.19348 12.19816 12.20307 12.20814 12.21333 12.21867 12.22422 12.23002
## [145] 12.23612 12.24255 12.24936 12.25659 12.26430 12.27251 12.28129 12.29066
## [153] 12.30067 12.31138 12.32281 12.33663 12.35407 12.37451 12.39735 12.42200
## [161] 12.44783 12.47426 12.50068 12.52648 12.55105 12.57381 12.59413 12.61141
## [169] 12.62506 12.63811 12.65377 12.67170 12.69156 12.71299 12.73566 12.75921
## [177] 12.78332 12.80763 12.83179 12.85547 12.87832 12.89999 12.92015 12.93844
## [185] 12.95453 12.96807 12.97871 12.98611 12.98993 12.98981 12.98659 12.98136
## [193] 12.97424 12.96536 12.95483 12.94276 12.92927 12.91448 12.89850 12.88144
## [201] 12.86343 12.84458 12.82500 12.80481 12.78413 12.76308 12.74176 12.72029
## [209] 12.69880 12.67313 12.63982 12.60005 12.55498 12.50580 12.45366 12.39974
## [217] 12.34520 12.29123 12.23898 12.18963 12.14435 12.10430 12.07066 12.03854
## [225] 12.00275 11.96401 11.92307 11.88066 11.83749 11.79431 11.75184 11.71082
## [233] 11.67197 11.63604 11.60373 11.57580 11.55296 11.53308 11.51359 11.49464
## [241] 11.47639 11.45896 11.44251 11.42716 11.41308 11.40040 11.38926 11.37980
## [249] 11.37217 11.36652 11.36297 11.36276 11.36667 11.37424 11.38499 11.39845
## [257] 11.41414 11.43160 11.45035 11.46991 11.48982 11.50960 11.52877 11.54687
## [265] 11.56342 11.57794 11.58996 11.60212 11.61708 11.63444 11.65377 11.67467
## [273] 11.69672 11.71952 11.74266 11.76572 11.78828 11.80995 11.83030 11.84893
## [281] 11.86543 11.88127 11.89812 11.91581 11.93418 11.95306 11.97230 11.99173
## [289] 12.01119 12.03052 12.04955 12.06812 12.08608 12.10325 12.11948 12.13490
## [297] 12.14980 12.16422 12.17819 12.19177 12.20499 12.21789 12.23052 12.24292
## [305] 12.25514 12.26721 12.27917 12.29108 12.30297 12.31488 12.32686 12.33895
## [313] 12.35119 12.36363 12.37656 12.39018 12.40438 12.41905 12.43408 12.44934
## [321] 12.46473 12.48014 12.49545 12.51054 12.52531 12.53964 12.55342 12.56654
## [329] 12.57888 12.59032 12.60151 12.61309 12.62495 12.63698 12.64909 12.66116
## [337] 12.67309 12.68478 12.69612 12.70701 12.71734 12.72701 12.73591 12.74394
## [345] 12.75097 12.75704 12.76222 12.76663 12.77037 12.77354 12.77624 12.77857
## [353] 12.78063 12.78253 12.78436 12.78623 12.78824 12.79048 12.79274 12.79473
## [361] 12.79645 12.79791 12.79912 12.80008 12.80080 12.80129 12.80155 12.80160
## [369] 12.80143 12.80105 12.80048 12.79972 12.79804 12.79486 12.79037 12.78477
## [377] 12.77824 12.77098 12.76319 12.75507 12.74680 12.73858 12.73060 12.72307
## [385] 12.71617 12.71010 12.70506 12.70123 12.69881 12.69800 12.69900 12.70188
## [393] 12.70644 12.71240 12.71947 12.72738 12.73585 12.74461 12.75338 12.76187
## [401] 12.76982 12.77695 12.78298 12.78763 12.79062 12.79346 12.79767 12.80299
## [409] 12.80915 12.81589 12.82294 12.83004 12.83692 12.84332 12.84898 12.85363
## [417] 12.85701 12.85886 12.85890 12.85785 12.85657 12.85508 12.85339 12.85150
## [425] 12.84942 12.84717 12.84474 12.84216 12.83941 12.83653 12.83350 12.83034
## [433] 12.82707 12.82315 12.81817 12.81226 12.80557 12.79825 12.79045 12.78230
## [441] 12.77396 12.76557 12.75728 12.74922 12.74156 12.73442 12.72797 12.72234
## [449] 12.71768 12.71333 12.70858 12.70349 12.69812 12.69254 12.68680 12.68098
## [457] 12.67512 12.66930 12.66357 12.65801 12.65266 12.64760 12.64288 12.63827
## [465] 12.63350 12.62861 12.62363 12.61858 12.61352 12.60845 12.60342 12.59846
## [473] 12.59360 12.58887 12.58430 12.57993 12.57579 12.57189 12.56821 12.56475
## [481] 12.56148 12.55837 12.55541 12.55259 12.54987 12.54724 12.54468 12.54217
## [489] 12.53969 12.53722 12.53474
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 491)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.88776 10.96214 11.03528 11.10717 11.17781 11.24718 11.31528 11.38208
##   [9] 11.44758 11.51177 11.57464 11.63617 11.69636 11.75519 11.81265 11.86874
##  [17] 11.92343 11.97672 12.02860 12.07905 12.12807 12.17567 12.22189 12.26674
##  [25] 12.31025 12.35246 12.39339 12.43306 12.47150 12.50875 12.54482 12.57974
##  [33] 12.61355 12.64627 12.67756 12.70714 12.73509 12.76150 12.78646 12.81003
##  [41] 12.83231 12.85339 12.87335 12.89226 12.91022 12.92731 12.94361 12.95921
##  [49] 12.97305 12.98419 12.99290 12.99946 13.00412 13.00717 13.00886 13.00948
##  [57] 13.00928 13.00854 13.00753 13.00652 13.00577 13.00556 13.00488 13.00260
##  [65] 12.99888 12.99382 12.98758 12.98027 12.97203 12.96299 12.95328 12.94303
##  [73] 12.93236 12.92142 12.91032 12.89921 12.88821 12.87745 12.86630 12.85409
##  [81] 12.84090 12.82682 12.81194 12.79635 12.78012 12.76335 12.74612 12.72852
##  [89] 12.71063 12.69254 12.67434 12.65611 12.63552 12.61067 12.58227 12.55107
##  [97] 12.51777 12.48310 12.44779 12.41255 12.37812 12.34521 12.31454 12.28685
## [105] 12.26285 12.24327 12.22488 12.20431 12.18205 12.15859 12.13443 12.11006
## [113] 12.08598 12.06267 12.04064 12.02037 12.00236 11.98711 11.97510 11.96683
## [121] 11.96107 11.95624 11.95236 11.94940 11.94736 11.94624 11.94604 11.94675
## [129] 11.94836 11.95087 11.95427 11.95856 11.96373 11.96979 11.97671 11.98451
## [137] 11.99316 12.00268 12.01305 12.02597 12.04281 12.06309 12.08634 12.11207
## [145] 12.13981 12.16906 12.19935 12.23021 12.26114 12.29167 12.32131 12.34960
## [153] 12.37603 12.40015 12.42146 12.44383 12.47093 12.50195 12.53609 12.57253
## [161] 12.61047 12.64912 12.68765 12.72528 12.76118 12.79457 12.82462 12.85054
## [169] 12.87152 12.89043 12.91051 12.93156 12.95335 12.97568 12.99831 13.02105
## [177] 13.04367 13.06595 13.08768 13.10864 13.12862 13.14740 13.16476 13.18049
## [185] 13.19437 13.20618 13.21571 13.22274 13.22706 13.22845 13.22750 13.22500
## [193] 13.22100 13.21556 13.20874 13.20058 13.19115 13.18051 13.16871 13.15580
## [201] 13.14185 13.12691 13.11103 13.09428 13.07670 13.05837 13.03932 13.01963
## [209] 12.99934 12.97560 12.94608 12.91163 12.87306 12.83123 12.78697 12.74112
## [217] 12.69452 12.64799 12.60239 12.55854 12.51729 12.47947 12.44592 12.41244
## [225] 12.37474 12.33357 12.28967 12.24376 12.19660 12.14890 12.10143 12.05490
## [233] 12.01005 11.96763 11.92837 11.89301 11.86229 11.83261 11.80032 11.76602
## [241] 11.73031 11.69381 11.65712 11.62084 11.58559 11.55197 11.52058 11.49203
## [249] 11.46694 11.44590 11.42952 11.41718 11.40770 11.40082 11.39634 11.39400
## [257] 11.39357 11.39482 11.39753 11.40144 11.40633 11.41197 11.41811 11.42454
## [265] 11.43100 11.43727 11.44312 11.45046 11.46109 11.47459 11.49054 11.50852
## [273] 11.52811 11.54889 11.57043 11.59233 11.61415 11.63548 11.65590 11.67499
## [281] 11.69232 11.71023 11.73102 11.75427 11.77955 11.80644 11.83450 11.86330
## [289] 11.89241 11.92142 11.94988 11.97737 12.00346 12.02773 12.04973 12.07100
## [297] 12.09325 12.11633 12.14012 12.16447 12.18925 12.21431 12.23952 12.26473
## [305] 12.28982 12.31465 12.33907 12.36294 12.38613 12.40850 12.42992 12.45023
## [313] 12.46931 12.48702 12.50376 12.52005 12.53590 12.55135 12.56641 12.58112
## [321] 12.59549 12.60955 12.62332 12.63683 12.65009 12.66314 12.67600 12.68869
## [329] 12.70123 12.71365 12.72575 12.73736 12.74852 12.75928 12.76968 12.77976
## [337] 12.78956 12.79914 12.80854 12.81779 12.82695 12.83605 12.84515 12.85429
## [345] 12.86300 12.87086 12.87798 12.88447 12.89043 12.89596 12.90118 12.90618
## [353] 12.91108 12.91598 12.92098 12.92620 12.93173 12.93768 12.94393 12.95025
## [361] 12.95662 12.96300 12.96937 12.97570 12.98196 12.98813 12.99417 13.00005
## [369] 13.00576 13.01126 13.01652 13.02151 13.02628 13.03090 13.03537 13.03971
## [377] 13.04392 13.04802 13.05200 13.05588 13.05968 13.06339 13.06702 13.07059
## [385] 13.07411 13.07758 13.08101 13.08441 13.08779 13.09116 13.09452 13.09844
## [393] 13.10331 13.10893 13.11509 13.12159 13.12823 13.13478 13.14105 13.14684
## [401] 13.15192 13.15611 13.15918 13.16094 13.16117 13.16065 13.16021 13.15978
## [409] 13.15928 13.15861 13.15771 13.15649 13.15488 13.15277 13.15011 13.14680
## [417] 13.14277 13.13794 13.13221 13.12490 13.11555 13.10447 13.09198 13.07838
## [425] 13.06398 13.04908 13.03401 13.01906 13.00455 12.99078 12.97806 12.96670
## [433] 12.95702 12.94761 12.93702 12.92545 12.91309 12.90016 12.88685 12.87337
## [441] 12.85992 12.84669 12.83389 12.82172 12.81039 12.80009 12.79103 12.78341
## [449] 12.77743 12.77223 12.76690 12.76149 12.75609 12.75077 12.74560 12.74066
## [457] 12.73602 12.73175 12.72794 12.72464 12.72194 12.71991 12.71862 12.71782
## [465] 12.71720 12.71680 12.71664 12.71676 12.71718 12.71794 12.71906 12.72057
## [473] 12.72251 12.72490 12.72778 12.73117 12.73510 12.73959 12.74464 12.75021
## [481] 12.75631 12.76289 12.76995 12.77746 12.78541 12.79377 12.80253 12.81166
## [489] 12.82114 12.83096 12.84110
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 491)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.87351 10.93191 10.98934 11.04580 11.10130 11.15581 11.20936 11.26192
##   [9] 11.31351 11.36411 11.41373 11.46236 11.51001 11.55666 11.60231 11.64697
##  [17] 11.69063 11.73329 11.77494 11.81559 11.85524 11.89392 11.93161 11.96833
##  [25] 12.00406 12.03881 12.07258 12.10537 12.13718 12.16801 12.19785 12.22671
##  [33] 12.25459 12.28149 12.30727 12.33183 12.35522 12.37747 12.39864 12.41875
##  [41] 12.43785 12.45599 12.47320 12.48953 12.50502 12.51971 12.53364 12.54686
##  [49] 12.55921 12.57054 12.58087 12.59023 12.59863 12.60612 12.61270 12.61841
##  [57] 12.62327 12.62730 12.63053 12.63299 12.63469 12.63567 12.63505 12.63209
##  [65] 12.62704 12.62013 12.61161 12.60171 12.59066 12.57871 12.56610 12.55307
##  [73] 12.53985 12.52668 12.51381 12.50146 12.48988 12.47931 12.46782 12.45360
##  [81] 12.43708 12.41871 12.39890 12.37810 12.35673 12.33523 12.31403 12.29356
##  [89] 12.27426 12.25655 12.24087 12.22764 12.21514 12.20146 12.18684 12.17149
##  [97] 12.15563 12.13948 12.12325 12.10718 12.09147 12.07635 12.06204 12.04875
## [105] 12.03671 12.02613 12.01681 12.00833 12.00059 11.99350 11.98695 11.98084
## [113] 11.97508 11.96957 11.96420 11.95888 11.95350 11.94798 11.94220 11.93608
## [121] 11.92945 11.92229 11.91472 11.90683 11.89875 11.89058 11.88243 11.87440
## [129] 11.86661 11.85917 11.85218 11.84575 11.83999 11.83502 11.83094 11.82785
## [137] 11.82587 11.82511 11.82568 11.82669 11.82729 11.82762 11.82785 11.82810
## [145] 11.82853 11.82928 11.83050 11.83234 11.83494 11.83844 11.84301 11.84877
## [153] 11.85589 11.86449 11.87474 11.88757 11.90352 11.92214 11.94295 11.96551
## [161] 11.98936 12.01404 12.03908 12.06404 12.08846 12.11187 12.13382 12.15384
## [169] 12.17149 12.19010 12.21298 12.23963 12.26957 12.30228 12.33729 12.37409
## [177] 12.41219 12.45110 12.49032 12.52935 12.56770 12.60488 12.64039 12.67373
## [185] 12.70441 12.73194 12.75582 12.77556 12.79066 12.80062 12.80704 12.81186
## [193] 12.81516 12.81699 12.81741 12.81649 12.81428 12.81085 12.80626 12.80056
## [201] 12.79383 12.78613 12.77750 12.76803 12.75776 12.74675 12.73508 12.72280
## [209] 12.70997 12.69274 12.66796 12.63679 12.60036 12.55983 12.51634 12.47102
## [217] 12.42503 12.37951 12.33561 12.29447 12.25723 12.22504 12.19904 12.17497
## [225] 12.14814 12.11905 12.08824 12.05620 12.02345 11.99051 11.95789 11.92611
## [233] 11.89567 11.86710 11.84089 11.81758 11.79767 11.77992 11.76275 11.74619
## [241] 11.73026 11.71499 11.70041 11.68654 11.67341 11.66105 11.64948 11.63873
## [249] 11.62882 11.61979 11.61165 11.60489 11.59990 11.59652 11.59462 11.59406
## [257] 11.59471 11.59643 11.59908 11.60252 11.60662 11.61123 11.61623 11.62146
## [265] 11.62681 11.63211 11.63725 11.64340 11.65164 11.66168 11.67322 11.68597
## [273] 11.69963 11.71391 11.72852 11.74315 11.75753 11.77134 11.78430 11.79612
## [281] 11.80650 11.81619 11.82615 11.83634 11.84675 11.85736 11.86814 11.87908
## [289] 11.89015 11.90133 11.91260 11.92394 11.93533 11.94675 11.95817 11.97051
## [297] 11.98456 12.00012 12.01698 12.03494 12.05379 12.07334 12.09338 12.11371
## [305] 12.13412 12.15440 12.17437 12.19381 12.21251 12.23029 12.24692 12.26222
## [313] 12.27597 12.28798 12.29875 12.30897 12.31865 12.32782 12.33651 12.34473
## [321] 12.35251 12.35988 12.36686 12.37347 12.37974 12.38569 12.39134 12.39672
## [329] 12.40186 12.40677 12.41109 12.41451 12.41714 12.41909 12.42049 12.42144
## [337] 12.42206 12.42247 12.42278 12.42311 12.42356 12.42427 12.42533 12.42687
## [345] 12.42833 12.42913 12.42937 12.42915 12.42855 12.42768 12.42662 12.42546
## [353] 12.42431 12.42326 12.42239 12.42181 12.42161 12.42188 12.42160 12.41985
## [361] 12.41688 12.41294 12.40828 12.40316 12.39782 12.39251 12.38748 12.38299
## [369] 12.37927 12.37660 12.37520 12.37534 12.37669 12.37874 12.38142 12.38467
## [377] 12.38845 12.39268 12.39731 12.40228 12.40754 12.41303 12.41868 12.42444
## [385] 12.43026 12.43606 12.44180 12.44742 12.45286 12.45806 12.46295 12.46861
## [393] 12.47591 12.48456 12.49426 12.50470 12.51558 12.52661 12.53748 12.54789
## [401] 12.55754 12.56612 12.57335 12.57891 12.58250 12.58518 12.58815 12.59129
## [409] 12.59453 12.59775 12.60086 12.60377 12.60637 12.60857 12.61027 12.61138
## [417] 12.61179 12.61142 12.61015 12.60752 12.60326 12.59762 12.59082 12.58310
## [425] 12.57471 12.56587 12.55683 12.54781 12.53906 12.53082 12.52331 12.51677
## [433] 12.51145 12.50630 12.50021 12.49333 12.48582 12.47782 12.46949 12.46098
## [441] 12.45244 12.44402 12.43586 12.42813 12.42097 12.41453 12.40897 12.40443
## [449] 12.40107 12.39827 12.39535 12.39235 12.38933 12.38633 12.38340 12.38059
## [457] 12.37794 12.37549 12.37331 12.37143 12.36990 12.36877 12.36808 12.36767
## [465] 12.36734 12.36711 12.36700 12.36704 12.36724 12.36763 12.36821 12.36902
## [473] 12.37008 12.37139 12.37299 12.37490 12.37713 12.37969 12.38258 12.38578
## [481] 12.38929 12.39308 12.39714 12.40146 12.40604 12.41085 12.41588 12.42112
## [489] 12.42657 12.43219 12.43799
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")